﻿@model iChip.Hotspots.Models.FieldModel
@{
    ViewBag.Title = "Thêm mới phương thức truy cập";
    Layout = "~/Views/Shared/_LayoutAdmin.cshtml";
    string[] color = { "blue", "green", "purple", "yellow" };
    int index = 0;
}
<!-- BEGIN SAMPLE PORTLET CONFIGURATION MODAL FORM-->

<div id="addField" class="modal fade" tabindex="-1" data-width="600">
    <div class="modal-dialog modal-wide">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                <h4 class="modal-title">Thêm Các Cấu Hỏi</h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-md-12" id="divSurvey">
                        <div class="portlet">
                            <div class="portlet-title">
                                
                            </div>
                            <div class="portlet-body" id="fieldcreate">

                                @*@if (Model == null || Model.Fields == null || Model.Fields.Count == 0)
                                    {
                                        Html.RenderAction("GenFieldSurvey", "Admin");
                                    }
                                    else
                                    {
                                        //Html.RenderPartial("_GenFieldSurvey", Model.Fields);
                                        Html.RenderPartial("_GenFieldSurvey");
                                    }*@
                                @{Html.RenderPartial("_GenFieldSurvey");}
                            </div>
                        </div>
                    </div>

                </div>
                <div class="modal-footer">
                    <div class="col-md-12 text-center">
                        <a onclick="AddField();" class="btn green"><i class="icon-plus"></i> Thêm Câu Hỏi</a>                      
                        <button type="button" class="btn green" id="btEditField" onclick="SaveField();">Cập nhật</button>
                        <button type="button" data-dismiss="modal" class="btn">Đóng</button>
                    </div>
                </div>

            </div>
        </div>

    </div>
</div>

<div id="addFieldValue" class="modal fade" tabindex="-1">
    <div class="modal-dialog">

    </div>
</div>
<!-- /.modal -->
<!-- END SAMPLE PORTLET CONFIGURATION MODAL FORM-->
<!-- BEGIN PAGE HEADER-->
<div class="row">
    <div class="col-md-12">
        <!-- BEGIN PAGE TITLE & BREADCRUMB-->
        <h3 class="page-title">
            Quản Lý Phương Thức Truy Cập
        </h3>
        <ul class="page-breadcrumb breadcrumb">
            <li>
                <i class="icon-home"></i>
                <a href="/Admin">Tổng quan</a>
                <i class="icon-angle-right"></i>
            </li>
            <li>
                <a href="javascript:window.history.back()">Danh sách Splash</a>
                <i class="icon-angle-right"></i>
            </li>
            <li><a href="">Quản Lý Phương Thức Truy Cập</a></li>
            <li class="pull-right">
            </li>
        </ul>
        <!-- END PAGE TITLE & BREADCRUMB-->
    </div>
</div>
<!-- END PAGE HEADER-->
@if (Model != null)
{
    using (Html.BeginForm("AddMethod", "Admin", FormMethod.Post, new { id = "formAddMethod" }))
    {
        Html.AntiForgeryToken();
        Html.ValidationSummary();
        <div class="row">
            <div class="col-md-12">
                <div class="portlet">                    
                    <div class="portlet-body">
                        <div class="form-inline" role="form">
                            <table class="table table-striped table-bordered table-advance table-hover" id="tbHotspots">
                                <thead>
                                    <tr>
                                        <th style="width:8px;">Chọn</th>
                                        <th>Phương Thức</th>
                                        <th>Thứ Tự</th>
                                        <th>Mô Tả</th>                                       
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach (var item in Model.Methods)
                                    {
                                        if (index > color.Length)
                                        {
                                            index = 0;
                                        }

                                        <tr class="odd gradeX">
                                            <td>@Html.CheckBoxFor(m => item.Method_Status, new { id = @item.Method_Type, @Value = @item.Method_Id, Name = "Method_Status" })</td>
                                            <td><a style="width:200px" data-methodid="@item.Method_Id" class="btn @color[index]" onclick="ShowModalField('@item.Method_Type', @item.Method_Id)">@item.Method_Type</a></td>
                                            <td>@Html.TextBoxFor(m => item.Method_Order, new { @class = "form-control", @placeholder = "Order", style = "width:50px", Name = "Method_Order" })</td>
                                            <td>@item.Method_Description</td>
                                        </tr>                                     
                                        index++;

                                    }
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-md-12">
                <div class="modal-footer">
                    <button onclick="SaveMethod();" type="button" class="btn green">Cập nhật</button>
                    <button type="button" data-dismiss="modal" class="btn" onclick="window.history.back();">Hủy</button>

                </div>
            </div>

        </div>

    }
}
@section Scripts {
    <script>
        var methodId = 0;
        var survey = '@iChip.Hotspots.Commons.Enums.MethodType.Survey';
        var objRequetsFieldValue = [];
        function ShowModalField(id, methodid) {
            if (id == 'Survey') {
                $("#addField").modal("show");
            }

            methodId = methodid;
        }

        function AddFieldValue(id){
            console.log($(id).data("field-id"));
            
            $.ajax({
                method: "POST",
                url: '@Url.Action("GenFieldValueSurvey","Admin")',
                data:{fieldId: parseInt($(id).data("field-id"))}
            })
            .done(function (msg) {
                $("#addFieldValue .modal-dialog").html(msg);
                App.init(); // initlayout and core plugins
                $("#addFieldValue").modal("show");
            });

        }

        function RemoveField(fieldId, el) {
            $.ajax({
                method: "POST",
                url: '@Url.Action("RemoveField", "Admin")',
                data:{ fieldId: fieldId}
            })
            .done(function (msg) {
                if(msg.Status){
                    $(el).closest("div.form-inline").remove();
                }
                else
                {
                    alert(msg.Message);
                    return false;
                }
            });
        }

        function AddField() {
            $.ajax({
                method: "GET",
                url: '@Url.Action("GenFieldSurvey","Admin")',// "/Admin/GenField",
            })
            .done(function (msg) {
                $("#fieldcreate").append(msg);
                App.init(); // initlayout and core plugins
            });
        }
        function AddFiledValueOther(fieldId){
            $.ajax({
                method: "GET",
                url: '@Url.Action("GenFieldValueSurveyOther","Admin")',
                data: { fieldId: fieldId }
            })
           .done(function (msg) {
               $("#addFieldValue .modal-body").append(msg);
               App.init(); // initlayout and core plugins
           });
        }


        function DelField(id) {

            var fieldId = parseInt($(id).data("field-id"));
            if(fieldId>0)
            {
                var conf = confirm("Trường này có thể đã có thông tin dữ liệu. Bạn có thực sự muốn xóa?");
                if(conf==true){
                    RemoveField(fieldId, id);
                }
            }
            else{
                $(id).closest("div.form-inline").remove();
            }

        }

        function SaveField() {
            var objRequest = [];
            var objForm = $("#fieldcreate form.form-inline");
            if ($(objForm).length > 0) {
                for (var i = 0; i < $(objForm).length; i++) {                    
                    if($($(objForm)[i]).find("input[name=Field_Name]").val()!="")
                    {
                        var objItem = {};
                        objItem.Id = $($(objForm)[i]).find("input[name=Field_Id]").val()
                        objItem.Name = $($(objForm)[i]).find("input[name=Field_Name]").val();
                        objItem.Value = parseInt($($(objForm)[i]).find("select[name=Field_Type]").val());
                        if ($($(objForm)[i]).find("input[name=Field_IsRequire]:checked").length > 0) {
                            objItem.IsRequire = true;
                        }
                        else {
                            objItem.IsRequire = false;
                        }
                        objItem.Method_Id = methodId;
                        objRequest.push(objItem);
                    }
                }
            }
            objRequest.FieldValues = objRequetsFieldValue;
            console.log(objRequest);

            $.ajax({
                method: "POST",
                url: "@Url.Action("FieldCreated", "Admin")",
                data: JSON.stringify({ objRequest: objRequest,  objRequestFieldValue: objRequetsFieldValue}),
                contentType: "application/json",
            })
            .done(function (msg) {
                if (msg != null) {
                    if (msg.Status == 1) {
                        $("#addField").modal("hide");
                        //window.location.href = '@ViewBag.ReturnUrl';
                    }
                    else {
                        alert(msg.Message);
                    }
                }
            });
        }

        function SaveFieldValue(){            
            var objForm = $("#addFieldValue div.modal-body div.row");            
            if($(objForm).length>0){
                for (var i = 0; i < $(objForm).length; i++) {                    
                    if($($(objForm)[i]).find("input[name=FieldValue_Value]").val()!="")
                    {
                        var objItem = {};
                        objItem.Field_Id = $($(objForm)[i]).find("input[name=Field_Id]").val();
                        objItem.FieldValue_Value = $($(objForm)[i]).find("input[name=FieldValue_Value]").val();
                        objItem.FieldValue_Id = $($(objForm)[i]).find("input[name=FieldValue_Id]").val();
                        objRequetsFieldValue.push(objItem);
                    }
                }
            }

            console.log("SaveFieldValue", objRequetsFieldValue);
            $("#addFieldValue").modal("hide");
        }

        function SaveMethod() {
            var objRequest = [];
            var objForm = $("#formAddMethod div.row-method");
            if ($(objForm).length > 0) {
                for (var i = 0; i < $(objForm).length; i++) {
                    var objItem = {};
                    objItem.Method_Order = parseInt($($(objForm)[i]).find("input[name=Method_Order]").val());
                    objItem.Method_Type = $($(objForm)[i]).find("input[name=Method_Type]").val();
                    objItem.Method_Id = parseInt($($(objForm)[i]).find("input[name=Method_Id]").val());
                    if ($($(objForm)[i]).find("input[name=Method_Status]:checked").length > 0) {
                        objItem.Method_Status = true;
                    }
                    else {
                        objItem.Method_Status = false;
                    }
                    objItem.Splash_Id = @ViewBag.Splash_Id;
                    objRequest.push(objItem);
                }
            }
            console.log(objRequest);
            $.ajax({
                url: '@Url.Action("AddMethod", "Admin")', //this is the submit URL
                type: 'POST', //or POST
                data: JSON.stringify({ model: objRequest }),
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function (data) {
                    if (data.Status == true) {
                        window.history.back();
                    }
                    else {
                        alert(data.Message);
                    }
                }
            });
        }

        $("#" + survey).on("click", function () {
            if ($(this).is(':checked')) {
                ShowModalField(survey, $(this).val());
            }
            else {

            }
        });

        $("[name=Field_Type]").each(function(){
            var idFieldType = parseInt($(this).find(":selected").val());
            $(this).unbind().bind('change', function(){                
                if(idFieldType==4 || idFieldType==5 || idFieldType==6 )
                {
                    $(this).parent().parent().find('.btn.btn-success').removeClass("hidden");
                }
                else{
                    $(this).parent().parent().find('.btn.btn-success').addClass("hidden");
                }        
            });
            if(idFieldType==4 || idFieldType==5 || idFieldType==6 )
            {
                $(this).parent().parent().find('.btn.btn-success').removeClass("hidden");
            }
            else{
                $(this).parent().parent().find('.btn.btn-success').addClass("hidden");
            }        
        });

    </script>
}

